package com.css.fxfzaqbz.modules.data.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.data.entity.GasstationData;
import com.css.fxfzaqbz.modules.data.queryParams.GasstationDataQueryParams;
import com.css.fxfzaqbz.modules.data.service.GasstationDataService;
import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 次生灾害危险源数据子模块 加油站数据
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class GasstationDataController {

    @Resource
    GasstationDataService gasstationDataService ;

    /**
     * 新增GasstationData
     * @param gasstationData
     * @return
     */
    @PostMapping("/saveGasstationData")
    public RestResponse saveGasstationData (@RequestBody GasstationData gasstationData){
        RestResponse restResponse = null ;
        try{
            restResponse = RestResponse.succeed(gasstationDataService.saveGasstationData(gasstationData));
            restResponse.setMessage("新增成功");
        }catch (Exception e){
            log.error("新增加油站数据失败",e);
            restResponse = RestResponse.fail("新增加油站数据失败!");
        }
        return restResponse;
    }

    /**
     * 全查询  模糊查询  条件未指定
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllGasstationData")
    public RestResponse getAllGasstationData(HttpServletRequest request, GasstationDataQueryParams queryParams){

        RestResponse restResponse = null;
        try{
//            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
//            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(Integer.parseInt(PlatformPageUtils.CUR_PAGE_DEF));
            queryParams.setPageSize(Integer.parseInt(PlatformPageUtils.PAGE_SIZE_DEF));
            restResponse = RestResponse.succeed(gasstationDataService.getAllGasstationData(queryParams));
        }catch (Exception e){
            log.error("查询加油站数据失败!",e);
            restResponse = RestResponse.fail("查询加油站数据失败");
        }
        return restResponse;
    }

    /**
     * 根据id查询GasstationData数据
     * @param id
     * @return
     */
    @GetMapping("/getGasstationData/{id}")
    public RestResponse<GasstationData> getGasstationData(@PathVariable("id") String id) {
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(gasstationDataService.getGasstationDataById(id));
        }catch (Exception e){
            log.error("获取加油站数据失败!",e);
            restResponse = RestResponse.fail("获取加油站数据失败!");
        }
        return restResponse;
    }


    /**
     * 逻辑删除，修改数据标志位
     * @param ids
     * @return
     */
    @PostMapping("/delGasstationData/{ids}")
    public RestResponse deleteElevation(@PathVariable("ids") List<String> ids) {
        RestResponse restResponse = null;
        try{
            gasstationDataService.deleteGasstationDatas(ids);
            restResponse = RestResponse.succeed();
        }catch (Exception e){
            log.error("删除加油站数据失败!",e);
            restResponse = RestResponse.fail("删除加油站数据失败!");
        }
        return restResponse;
    }

    /**
     * 修改
     * @param gasstationDataVO
     * @return
     */
//    @PostMapping("/updateGasstationData")
//    public RestResponse<ElevationVO> updateElevation(@RequestBody GasstationDataVO gasstationDataVO)  {
//        RestResponse restResponse = null;
//        try{
//            restResponse = RestResponse.succeed(gasstationDataService.updateGasstationData(gasstationDataVO));
//        }catch (Exception e){
//            log.error("修改失败!",e);
//            restResponse = RestResponse.fail("修改失败!");
//        }
//        return restResponse;
//    }

    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcelGasstationData")
    public void exportExcel(HttpServletResponse response ) throws IOException {
        RestResponse restResponse = null ;
        try{
            List<GasstationData> list  = gasstationDataService.getAll();
            FileUtil2.exportExcel(list, "加油站数据", "sheet", GasstationData.class, "GasstationData", response);
            restResponse = RestResponse.succeed("导出成功");
        }catch (Exception e){
            log.error("导出失败!",e);
            restResponse = RestResponse.fail("导出失败");
        }

    }

    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelGasstationData")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        RestResponse restResponse = null;
        try {
            gasstationDataService.importExcel(file);
        } catch (Exception e) {
            log.error("导入数据失败", e);
            restResponse = RestResponse.fail("导入数据失败!");
        }
        return restResponse;
    }
}
